package br.com.werp.regras.suporte.controle;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import br.com.werp.regras.geral.FabricaSubSistema;
import br.com.werp.regras.geral.Relatorio;
import br.com.werp.regras.geral.util.DummyVO;
import br.com.werp.regras.geral.util.DummyVOInteger;
import br.com.werp.regras.geral.util.DummyVOString;
import br.com.werp.regras.geral.util.JRDataSourcePadrao;
import br.com.werp.regras.geral.util.Util;


public class DummySubReports {
    public static void main(String[] args) throws Exception {
        HashMap parametros = new HashMap();

        parametros.put(Relatorio.IMAGE_URI, "/suporte/imgJasper/");

        Map imagemMap = new HashMap();
        imagemMap.put(Relatorio.LOGO, "Logo.gif");

        Map subRelatorios = new HashMap();
        subRelatorios.put("DummySubString", "DummySubString");
        subRelatorios.put("DummySubInteger", "DummySubInteger");

        List linhas = geraListaDummyVO();

        Relatorio relatorio = FabricaSubSistema.getRelatorio();

        Util.verRelartorio(relatorio.processarRelatorio(new JRDataSourcePadrao(
                    linhas) {
                public Object getFieldValue(JRField jrField)
                    throws JRException {
                    DummyVO dummyVO = (DummyVO) valorAtual;

                    if ("dummyListInteger".equals(jrField.getName())) {
                        return new JRBeanCollectionDataSource(
                                dummyVO.getDummyListInteger());
                    } else if ("dummyListString".equals(jrField.getName())) {
                        return new JRBeanCollectionDataSource(
                                dummyVO.getDummyListString());
                    }

                    return super.getFieldValue(jrField);
                }
            }, "Dummy", Relatorio.TIPO_HTML, parametros,
                FabricaSubSistema.getSuporte(), subRelatorios, imagemMap));
    }

    private static List geraListaDummyVO() {
        int lotes = geradorInts(15);
        List retorno = new ArrayList();

        for (int i = 0; i < lotes; i++) {
            DummyVO dummyVO = new DummyVO();
            dummyVO.setValue1(i);
            dummyVO.setValue2("Valor " + i);
            dummyVO.setValue3(new Timestamp(System.currentTimeMillis()));

            int srtCont = geradorInts(8);

            for (int j = 0; j < srtCont; j++) {
                DummyVOString dummyVOString = new DummyVOString();
                dummyVOString.setValue1("Meu Dummy VO " + i);
                dummyVOString.setValue2("Meu Contador " + j);
                dummyVO.getDummyListString().add(dummyVOString);
            }

            int intCont = geradorInts(4);

            for (int x = 0; x < intCont; x++) {
                DummyVOInteger dummyVOInteger = new DummyVOInteger();
                dummyVOInteger.setValue(new Integer(x));
                dummyVO.getDummyListInteger().add(dummyVOInteger);
            }

            retorno.add(dummyVO);
        }

        return retorno;
    }

    private static int geradorInts(int ateQuanto) {
        return (int) (1 + (Math.random() * ateQuanto));
    }
}
